package cn.agiledata.bank.transaction.logic;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.IOUtil;
import cn.agiledata.bank.transaction.exception.TransactionException;

/**
 * 批量导入文件
 * 
 * @author maxianbo
 * 
 */
public class BatchImportFile {
    private String version;

    private String accountId;

    private Date createTime;

    private String purpose;

    private String remark;

    private List transList;

    BatchImportFile() {
        this.transList = new ArrayList();
    }

    private static String getTitleData(InputStream in, String titleName)
            throws IOException, TransactionException {
        String line = IOUtil.readLine(in);
        String[] data = (line + " ").split(":");
        if (data.length != 2 || !titleName.equalsIgnoreCase(data[0])) {
            throw TransactionException.getImportFileContentFormatException(
                    "Title", line);
        }
        return data[1].trim();
    }

    void loadTitleData(InputStream in) throws IOException, TransactionException {
        // 版本号
        this.version = getTitleData(in, "Version");
        // 账户编号
        this.accountId = getTitleData(in, "AccountId");
        // 文件创建时间
        String date = getTitleData(in, "Date");
        String time = getTitleData(in, "Time");
        Date createTime = DateUtil.string2Date(date + time, "yyyyMMddHHmmss");
        if (createTime == null) {
            throw TransactionException.getImportFileContentFormatException(
                    "DateTime", date + time);
        }
        this.createTime = createTime;
        // 资金用途
        this.purpose = getTitleData(in, "Purpose");
        // 备注
        this.remark = getTitleData(in, "Remark");
    }

    void loadTransData(InputStream in) throws IOException, TransactionException {
        // 跳转到交易数据行
        for (int i = 0; i < 2; i++) {
            IOUtil.readLine(in);
        }
        // 交易
        for (String line = IOUtil.readLine(in); line != null; line = IOUtil
                .readLine(in)) {
            System.out.println("Loading line: " + line);
            this.transList.add(BatchImportTrans.getInstance(line));
        }
    }

    /**
     * 读取批量导入文件输入流，解析并加载所有交易数据，生成批量导入文件对象。该输入流不会被关闭
     * 
     * @param in
     *            批量导入文件输入流
     * @return 批量导入文件
     * @throws TransactionException
     *             如果加载过程中出现错误
     */
    public static BatchImportFile getInstance(InputStream in)
            throws TransactionException {
        try {
            BatchImportFile file = new BatchImportFile();
            file.loadTitleData(in);
            file.loadTransData(in);
            return file;
        } catch (IOException e) {
            throw TransactionException.getImportFileIOException(e);
        }
    }

    /**
     * 
     * @return 版本号
     */
    public String getVersion() {
        return this.version;
    }

    /**
     * 
     * @return 付款方账户编号
     */
    public String getAccountId() {
        return this.accountId;
    }

    /**
     * 
     * @return 批量导入文件创建时间
     */
    public Date getCreateTime() {
        return this.createTime;
    }

    /**
     * 
     * @return 资金用途
     */
    public String getPurpose() {
        return this.purpose;
    }

    /**
     * 
     * @return 用途
     */
    public String getRemark() {
        return this.remark;
    }

    /**
     * 
     * @return 批量导入交易数目
     */
    public int getCount() {
        return this.transList.size();
    }

    /**
     * 获取指定索引处的批量导入交易
     * 
     * @param index
     *            索引
     * @return 指定索引处的批量导入交易
     */
    public BatchImportTrans getTrans(int index) {
        return (BatchImportTrans) this.transList.get(index);
    }
}
